package com.lanhao.aot.workhour;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;

import org.apache.commons.lang3.StringUtils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpUtil;

public class WorkHourFlush {
	public static void main(String[] args) {
		String loginUrl = "http://iptv.iflytek.com/IPTV-Repair-service-project/login";
		String loginToken = StringUtils.EMPTY;
		JSONObject loginPayLoad = new JSONObject();
		loginPayLoad.put("userName", "周昆");
		loginPayLoad.put("password", "123456");
		String loginResult = HttpUtil.post(loginUrl, loginPayLoad.toString());
		if (StringUtils.isNotBlank(loginResult)) {
			JSONObject loginResultObject = JSON.parseObject(loginResult);
			if (loginResultObject != null) {
				JSONObject dataObject = loginResultObject.getJSONObject("data");
				loginToken = dataObject != null ? dataObject.getString("token") : StringUtils.EMPTY;
			}
		}

		System.out.println("登录成功，获取到token=" + loginToken);

		if (StringUtils.isBlank(loginToken)) {
			System.out.println("登陆失败，直接返回");
			return;
		}

		String workUrl = "http://iptv.iflytek.com/IPTV-Repair-service-project/api/v1/workhours";
		List<String> everyday = findDaysStr("2020-06-20", "2020-08-01");
		for (String day : everyday) {
			boolean weekend = true;
			try {
				weekend = isWeekend(day);
			} catch (ParseException e) {
				e.printStackTrace();
			}
			if (weekend) {
				System.out.println("日期day=" + day + "是周末，跳过填报工作");
				continue;
			}
			JSONObject payLoad = new JSONObject();
			payLoad.put("projectId", 167);
			payLoad.put("projectName", "智能家居-智能家居产品研发");
			payLoad.put("subId", 233);
			payLoad.put("subName", "");
			payLoad.put("workDate", day);
			payLoad.put("workHour", "8");
			payLoad.put("workContent", "1、智能家居平台日常需求评审与方案设计\n2、研发日常管理与设计评审、代码评审\n3、二期工作量核算与工作实施");
			payLoad.put("userName", "周昆");

			String result = HttpRequest.post(workUrl).header("token", loginToken)// 头信息，多个头信息多次调用此方法即可
					.header("User-Agent",
							"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.106 Safari/537.36")
					.body(payLoad.toString()).timeout(20000)// 超时，毫秒
					.execute().body();

			System.out.println("day=" + day + "调用工时系统返回结果：result=" + result);

			try {
				TimeUnit.SECONDS.sleep(5);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}

	}

	public static List<String> findDaysStr(String begintTime, String endTime) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Date dBegin = null;
		Date dEnd = null;
		try {
			dBegin = sdf.parse(begintTime);
			dEnd = sdf.parse(endTime);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		List<String> daysStrList = new ArrayList<String>();
		daysStrList.add(sdf.format(dBegin));
		Calendar calBegin = Calendar.getInstance();
		calBegin.setTime(dBegin);
		Calendar calEnd = Calendar.getInstance();
		calEnd.setTime(dEnd);
		while (dEnd.after(calBegin.getTime())) {
			calBegin.add(Calendar.DAY_OF_MONTH, 1);
			String dayStr = sdf.format(calBegin.getTime());
			daysStrList.add(dayStr);
		}
		return daysStrList;
	}

	public static boolean isWeekend(String bDate) throws ParseException {
		DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		Date bdate = format.parse(bDate);
		Calendar cal = Calendar.getInstance();
		cal.setTime(bdate);
		if (cal.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY || cal.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY) {
			return true;
		} else {
			return false;
		}
	}
}
